package org.ws.task.zookeeper.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ws.task.event.support.startup.BootstrapEvent;
import org.ws.task.event.support.startup.BootstrapEventType;
import org.ws.task.event.support.BootstrapEventHolder;
import org.ws.task.listener.zk.CompleteNodeListener;
import org.ws.task.model.NodeMetadata;
import org.ws.task.model.TaskConfig;

/**
 * 执行发送事件的Listener
 */
public class NodeEventListener implements CompleteNodeListener {

    @Override
    public void becomeLeader(final NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.BECOME_LEADER));
    }

    @Override
    public void notLeader(NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.NOT_LEADER));
    }

    @Override
    public void changeLeaderNode(final NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.CHANGE_LEADER));
    }

    @Override
    public void removeLeaderNode(final NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.REMOVE_LEADER));
    }

    @Override
    public void updateTaskConfig(final TaskConfig taskConfig) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(taskConfig, BootstrapEventType.UPDATE_TASK_CONFIG));
    }

    @Override
    public void removeTaskConfig(final TaskConfig taskConfig) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(taskConfig, BootstrapEventType.REMOVE_TASK_CONFIG));
    }

    @Override
    public void updateWorkerNode(final NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.UPDATE_WORKER));
    }

    @Override
    public void removeWorkerNode(final NodeMetadata nodeMetadata) {
        BootstrapEventHolder.getInstance().publishEvent(new BootstrapEvent(nodeMetadata, BootstrapEventType.REMOVE_WORKER));
    }
}
